﻿

using DualPlaneFR4Paster.Common.Services;
using DualPlaneFR4Paster.Extensions;
using DualPlaneFR4Paster.Models;
using Newtonsoft.Json.Linq;
using Prism.Commands;
using Prism.Ioc;
using Prism.Services.Dialogs;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace DualPlaneFR4Paster.ViewModels.Dialogs
{
    public class PLCDialogViewModel : DialogViewModel
    {
        #region 变量
        private IPLCModbusService plcModbus;
        private IGTSCardService Top_A4;
        private IGTSCardService Top_A8;
        private IStepMotorModbusService stepMotorModbus;
        string partNum = string.Empty;
        private CancellationTokenSource source;
        #endregion
        #region 属性
        private ObservableCollection<bool> xCoils;
        public ObservableCollection<bool> XCoils
        {
            get { return xCoils; }
            set { SetProperty(ref xCoils, value); }
        }
        private ObservableCollection<bool> mCoils;
        public ObservableCollection<bool> MCoils
        {
            get { return mCoils; }
            set { SetProperty(ref mCoils, value); }
        }
        private int stepMotorHiSpeed;
        public int StepMotorHiSpeed
        {
            get { return stepMotorHiSpeed; }
            set { SetProperty(ref stepMotorHiSpeed, value); }
        }
        private int stepMotorLowSpeed;
        public int StepMotorLowSpeed
        {
            get { return stepMotorLowSpeed; }
            set { SetProperty(ref stepMotorLowSpeed, value); }
        }
        #endregion
        #region 命令
        private DelegateCommand<object> jog_MouseDownCommand;
        public DelegateCommand<object> Jog_MouseDownCommand =>
            jog_MouseDownCommand ?? (jog_MouseDownCommand = new DelegateCommand<object>(ExecuteJog_MouseDownCommand));
        private DelegateCommand jog_MouseUpCommand;
        public DelegateCommand Jog_MouseUpCommand =>
            jog_MouseUpCommand ?? (jog_MouseUpCommand = new DelegateCommand(ExecuteJog_MouseUpCommand));
        private DelegateCommand<object> setStepMotorSpeedCommand;
        public DelegateCommand<object> SetStepMotorSpeedCommand =>
            setStepMotorSpeedCommand ?? (setStepMotorSpeedCommand = new DelegateCommand<object>(ExecuteSetStepMotorSpeedCommand));
        private DelegateCommand<object> mIOOutCommand;
        public DelegateCommand<object> MIOOutCommand =>
            mIOOutCommand ?? (mIOOutCommand = new DelegateCommand<object>(ExecuteMIOOutCommand));

        void ExecuteMIOOutCommand(object obj)
        {
            plcModbus.SetM(1, (ushort)(2010 + int.Parse(obj.ToString())), MCoils[8 + int.Parse(obj.ToString())]);
        }
        void ExecuteSetStepMotorSpeedCommand(object obj)
        {
            try
            {
                switch (obj.ToString())
                {
                    case "快速":
                        {
                            stepMotorModbus.SetMotorSpeed(19, 77, StepMotorHiSpeed);
                            stepMotorModbus.SetMotorSpeed(20, 77, StepMotorHiSpeed);
                            using (var db = new DTContext(partNum))
                            {
                                var param = db.MParams.FirstOrDefault(p => p.Name == "StepMotorHiSpeed");
                                if (param != null)
                                {
                                    param.Value = StepMotorHiSpeed.ToString();
                                    db.SaveChanges();
                                }
                            }
                            _dialogHostService.MessageBox(MessageType.Message, "信息", "轨道速度设置完成");
                        }
                        break;
                    case "慢速":
                        {
                            stepMotorModbus.SetMotorSpeed(19, 77, StepMotorLowSpeed);
                            stepMotorModbus.SetMotorSpeed(20, 77, StepMotorLowSpeed);
                            using (var db = new DTContext(partNum))
                            {
                                var param = db.MParams.FirstOrDefault(p => p.Name == "StepMotorLowSpeed");
                                if (param != null)
                                {
                                    param.Value = StepMotorLowSpeed.ToString();
                                    db.SaveChanges();
                                }
                            }
                            _dialogHostService.MessageBox(MessageType.Message, "信息", "轨道速度设置完成");
                        }
                        break;
                    default:
                        break;
                }
            }
            catch (System.Exception ex)
            {
                _dialogHostService.MessageBox(MessageType.Message, "信息", ex.Message);
            }

        }
        void ExecuteJog_MouseUpCommand()
        {
            GTSCard.SetDo(Top_A8.A1.CardNo, 12, 1);
            GTSCard.SetDo(Top_A8.A1.CardNo, 13, 1);
            GTSCard.SetDo(Top_A4.A1.CardNo, 0, 1);
            GTSCard.SetDo(Top_A4.A1.CardNo, 1, 1);
        }
        void ExecuteJog_MouseDownCommand(object obj)
        {
            int index = int.Parse(obj.ToString()) - 1;
            if (index % 2 == 0)
            {
                GTSCard.SetDo(Top_A8.A1.CardNo, 12, 0);
                GTSCard.SetDo(Top_A8.A1.CardNo, 13, 1);
                GTSCard.SetDo(Top_A4.A1.CardNo, 0, 0);
                GTSCard.SetDo(Top_A4.A1.CardNo, 1, 1);
            }
            else
            {
                GTSCard.SetDo(Top_A8.A1.CardNo, 12, 1);
                GTSCard.SetDo(Top_A8.A1.CardNo, 13, 0);
                GTSCard.SetDo(Top_A4.A1.CardNo, 0, 1);
                GTSCard.SetDo(Top_A4.A1.CardNo, 1, 0);
            }
        }
        #endregion
        #region 导航
        public override void OnDialogOpened(IDialogParameters parameters)
        {
            try
            {
                partNum = parameters.GetValue<string>("PartNum");
                using (var db = new DTContext(partNum))
                {
                    var param = db.MParams.FirstOrDefault(p => p.Name == "StepMotorHiSpeed");
                    if (param != null)
                    {
                        StepMotorHiSpeed = int.Parse(param.Value);
                    }
                    param = db.MParams.FirstOrDefault(p => p.Name == "StepMotorLowSpeed");
                    if (param != null)
                    {
                        StepMotorLowSpeed = int.Parse(param.Value);
                    }
                }
            }
            catch (System.Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            source = new CancellationTokenSource();
            CancellationToken token = source.Token;
            Task.Run(()=> RunAction(token), token);
        }
        public override void OnDialogClosed()
        {
            if (source != null)
            {
                source.Cancel();
            }
        }
        #endregion
        public PLCDialogViewModel(IContainerProvider containerProvider) : base(containerProvider)
        {
            XCoils = new ObservableCollection<bool>();
            for (int i = 0; i < 84; i++)
            {
                XCoils.Add(false);
            }
            MCoils = new ObservableCollection<bool>();
            for (int i = 0; i < 16; i++)
            {
                MCoils.Add(false);
            }
            StepMotorHiSpeed = 0;
            StepMotorLowSpeed = 0;
            plcModbus = containerProvider.Resolve<IPLCModbusService>("PLCModbus");
            stepMotorModbus = containerProvider.Resolve<IStepMotorModbusService>("StepMotorModbus");
            Top_A4 = containerProvider.Resolve<IGTSCardService>("Top_A4");
            Top_A8 = containerProvider.Resolve<IGTSCardService>("Top_A8");
        }
        #region 功能函数
        private void RunAction(CancellationToken token)
        {
            while (true)
            {
                if (token.IsCancellationRequested)
                {
                    return;
                }
                XCoils[0] = plcModbus.XCoils_X0[0];
                XCoils[1] = plcModbus.XCoils_X0[1];
                XCoils[2] = plcModbus.XCoils_X0[2];
                XCoils[3] = plcModbus.XCoils_X0[3];
                XCoils[4] = plcModbus.XCoils_X0[4];
                XCoils[5] = plcModbus.XCoils_X0[5];
                XCoils[6] = plcModbus.XCoils_X0[6];
                XCoils[7] = plcModbus.XCoils_X0[7];
                XCoils[8] = plcModbus.XCoils_X0[8];
                XCoils[9] = plcModbus.XCoils_X0[9];
                XCoils[10] = plcModbus.XCoils_X0[10];
                XCoils[11] = plcModbus.XCoils_X10200[0];

                MCoils[0] = plcModbus.MCoils_M2000[2];
                MCoils[1] = plcModbus.MCoils_M2000[3];
                MCoils[2] = plcModbus.MCoils_M2000[4];
                MCoils[3] = plcModbus.MCoils_M2000[5];
                MCoils[4] = plcModbus.MCoils_M2000[6];
                MCoils[5] = plcModbus.MCoils_M2000[7];
                MCoils[6] = plcModbus.MCoils_M2000[8];
                MCoils[7] = plcModbus.MCoils_M2000[9];

                MCoils[8] = plcModbus.MCoils_M2000[10];
                MCoils[9] = plcModbus.MCoils_M2000[11];
                MCoils[10] = plcModbus.MCoils_M2000[12];
                MCoils[11] = plcModbus.MCoils_M2000[13];
                MCoils[12] = plcModbus.MCoils_M2000[14];
                MCoils[13] = plcModbus.MCoils_M2000[15];
                MCoils[14] = plcModbus.MCoils_M2000[16];
                MCoils[15] = plcModbus.MCoils_M2000[17];
                System.Threading.Thread.Sleep(200);
            }
        }
        #endregion
    }
}
